package zhoug.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View.MeasureSpec;

import zhoug.common.R;

import java.text.MessageFormat;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 限制View最大高度/宽度的工具
 *
 * @Author 35574
 * @Date 2020/7/21
 * @Description
 */
 class MaxViewSizeHelper {
    private static final String TAG = ">>>MaxViewSizeHelper";
    private static  boolean debug = false;

    private int maxHeight;
    private int maxWidth;

    public static void setDebug(boolean debug) {
        MaxViewSizeHelper.debug = debug;
    }

    public MaxViewSizeHelper(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        parseStyle(context, attrs, defStyleAttr);
    }

    /**
     * 在构造器中调用解析xml中的zg_maxHeight属性
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    private void parseStyle(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        int[] styleableRes = new int[]{R.attr.zg_maxHeight,R.attr.zg_maxWidth};
        TypedArray a = context.obtainStyledAttributes(attrs, styleableRes, 0, defStyleAttr);
        maxHeight = a.getDimensionPixelOffset(0, 0);
        maxWidth = a.getDimensionPixelOffset(1, 0);
        a.recycle();
    }

    /**
     * 计算限制高度后的MeasureSpec,
     *
     * @param heightMeasureSpec
     * @return heightMeasureSpec
     */
    public int getHeightMeasureSpec(int heightMeasureSpec) {
        if (maxHeight > 0 && MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY) {
            int height = MeasureSpec.getSize(heightMeasureSpec);
            int max=maxHeight;
            if(height>0){
                max = Math.min(maxHeight, height);
            }
            if (debug) {
                Log.d(TAG, MessageFormat.format("height={0},maxHeight={1},max={2}", height, maxHeight, max));
            }
            return MeasureSpec.makeMeasureSpec(max, MeasureSpec.AT_MOST);
        } else {
            if (debug) {
                Log.d(TAG, "onMeasure:super.onMeasure()");
            }
            return heightMeasureSpec;
        }
    }

    public int getWidthMeasureSpec(int widthMeasureSpec) {
        if (maxWidth > 0 && MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int max=maxWidth;
            if(width>0){
                max = Math.min(maxWidth, width);
            }
            if (debug) {
                Log.d(TAG, MessageFormat.format("width={0},maxWidth={1},max={2}", width, maxWidth, max));
            }
            return MeasureSpec.makeMeasureSpec(max, MeasureSpec.AT_MOST);
        } else {
            if (debug) {
                Log.d(TAG, "onMeasure:super.onMeasure()");
            }
            return widthMeasureSpec;
        }
    }


    public int getMaxHeight() {
        return maxHeight;
    }

    public void setMaxHeight(int maxHeight) {
        if (maxHeight >= 0) {
            this.maxHeight = maxHeight;
        }
    }

    public int getMaxWidth() {
        return maxWidth;
    }

    public void setMaxWidth(int maxWidth) {
        if(maxWidth>=0){
            this.maxWidth = maxWidth;
        }
    }

}